home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
System
/
ScreenDaemon 1.2
/
ScreenDaemon cdev
/
ScreenDaemon Control.cp
< prev
next >
Wrap
Text File
|
1996-05-07
|
19KB
|
614 lines
/**********************************************************
* Basic Black © 1993,1994 by Mason L. Bliss
* All Rights Reserved
* version 1.4.1
*
* The important bits involve the use of the Gestalt manager
* for cdev/INIT communication, to which the cdev mechanism
* itself is irrelevant.
**********************************************************/
#include <Folders.h>
#include <GestaltEqu.h>
#include "ScreenDaemon Control.h"
#include "SD.h"
// prototypes for 2 required procedures
long runable(void);
TControlPanel *makeCDEV(short numItems,DialogPtr cp);
// return 1 if we can run, otherwise 0
long runable(void)
{
return 1;
}
// code that allocates our cdev
TControlPanel *makeCDEV(short numItems,DialogPtr cp)
{
return new SDcdev(numItems,cp);
}
/**********************************************************
* SDcdev constructor
**********************************************************/
SDcdev::SDcdev(short numItems,DialogPtr cp) : TControlPanel(numItems,cp)
{
}
/**********************************************************
* SDcdev destructor
**********************************************************/
SDcdev::~SDcdev(void)
{
}
/**********************************************************
* SDcdev::Init
*
* This method initializes stuff.
**********************************************************/
long SDcdev::Init(void)
{
PrefStructureHandle prefHandle;
ControlHandle theControl;
short controlValue, theResFile;
long theLong, width, height;
Rect box;
Str255 theString;
Boolean usingPrefsFile;
/* Initialize stuff */
inherited::Init();
/* Load & Lock Picture Resources */
pictHandles[0] = GetResource('PICT', topLeftPict);
pictHandles[1] = GetResource('PICT', topRightPict);
pictHandles[2] = GetResource('PICT', botRightPict);
pictHandles[3] = GetResource('PICT', botLeftPict);
pictHandles[4] = GetResource('PICT', noArrowPict);
pictHandles[5] = GetResource('PICT', upArrowPict);
pictHandles[6] = GetResource('PICT', dnArrowPict);
pictHandles[7] = GetResource('PICT', grArrowPict);
HLock(pictHandles[0]);
HLock(pictHandles[1]);
HLock(pictHandles[2]);
HLock(pictHandles[3]);
HLock(pictHandles[4]);
HLock(pictHandles[5]);
HLock(pictHandles[6]);
HLock(pictHandles[7]);
/* Load the preferences */
theResFile = CurResFile();
usingPrefsFile = OpenPrefsFile();
prefHandle = (PrefStructureHandle) GetIndResource('PREF', 1);
if (ResError()) {
return 1;
}
/* get the sleep corner info */
sleepNowRect = (**prefHandle).sleepNowCorner;
GetDialogItem(fDialog, fLastItem - initDev + sleepNow,
&controlValue, (Handle *) &theControl, &box);
SetDItem(fDialog, fLastItem - initDev + sleepNow,
controlValue, pictHandles[sleepNowRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
width = ((box.right - box.left) / 2);
height = ((box.bottom - box.top) / 2);
/* Set up some small rects */
smallSNRect[0] = box;
smallSNRect[0].right -= width;
smallSNRect[0].bottom -= height;
smallSNRect[1] = box;
smallSNRect[1].left += width;
smallSNRect[1].bottom -= height;
smallSNRect[2] = box;
smallSNRect[2].left += width;
smallSNRect[2].top += height;
smallSNRect[3] = box;
smallSNRect[3].right -= width;
smallSNRect[3].top += height;
/* get the wake corner info */
neverSleepRect = (**prefHandle).sleepNeverCorner;
GetDItem(fDialog, fLastItem - initDev + neverSleep,
&controlValue, (Handle *) &theControl, &box);
SetDItem(fDialog, fLastItem - initDev + neverSleep,
controlValue, pictHandles[neverSleepRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
width = ((box.right - box.left) / 2);
height = ((box.bottom - box.top) / 2);
/* Set up more small rects */
smallNSRect[0] = box;
smallNSRect[0].right -= width;
smallNSRect[0].bottom -= height;
smallNSRect[1] = box;
smallNSRect[1].left += width;
smallNSRect[1].bottom -= height;
smallNSRect[2] = box;
smallNSRect[2].left += width;
smallNSRect[2].top += height;
smallNSRect[3] = box;
smallNSRect[3].right -= width;
smallNSRect[3].top += height;
/* get the "screen saver on" checkbox */
GetDItem(fDialog, fLastItem - initDev + screenOn, &controlValue, (Handle *) &theControl, &box);
if ((**prefHandle).saverOn)
SetCtlValue(theControl, 1);
/* get the "Show Startup Icon" checkbox */
GetDItem(fDialog, fLastItem - initDev + startupIcon, &controlValue, (Handle *) &theControl, &box);
if ((**prefHandle).showIcon)
SetCtlValue(theControl, 1);
/* get the idle time edit text item */
GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
theLong = (long) (**prefHandle).idleTime;
NumToString(theLong, theString);
SetIText((Handle) theControl, theString);
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
GetDItem(fDialog, fLastItem - initDev + idleRect, &controlValue, (Handle *) &theControl, &box);
height = ((box.bottom - box.top) / 2);
smIdleRect = box;
smIdleRect.bottom -= height;
/* Clean up */
ReleaseResource((Handle) prefHandle);
if (usingPrefsFile) {
CloseResFile(CurResFile());
UseResFile(theResFile);
}
/* see if the idle time arrow needs to be grey */
if (Gestalt('sysv', &theLong) != noErr || theLong < 0x700) {
GetDItem(fDialog, fLastItem - initDev + idleRect,
&controlValue, (Handle *) &theControl, &box);
SetDItem(fDialog, fLastItem - initDev + idleRect,
controlValue, pictHandles[grArrow], &box);
}
return 0;
}
/**********************************************************
* SDcdev::Idle
*
* Fix the EditText items if needed.
**********************************************************/
long SDcdev::Idle(void)
{
ControlHandle theControl;
short controlValue;
long theLong;
Rect box;
Str255 theString;
if (mustRemember) {
mustRemember = false;
/* fix the idle time information if needed */
GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
GetIText((Handle) theControl, theString);
StringToNum(theString, &theLong);
if (theLong < 1) {
SetIText((Handle) theControl, "\p1");
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
} else if (theLong > 60) {
SetIText((Handle) theControl, "\p60");
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
}
RememberValues();
}
return 0;
}
/**********************************************************
* SDcdev::Close
*
* This method releases the 'PICT' resources.
**********************************************************/
long SDcdev::Close(void)
{
short i;
for (i = 0; i < 7; ++i) {
HUnlock(pictHandles[i]);
ReleaseResource(pictHandles[i]);
}
inherited::Close();
return 0;
}
/**********************************************************
* SDcdev::KeyDown
*
* This filters out invalid key presses and stuff.
**********************************************************/
long SDcdev::KeyDown(short theKey)
{
if (((theKey < '0') || (theKey > '9')) && (theKey != 8)
&& (theKey != 9) && ((theKey < 28) || (theKey > 31))) {
fEvent->what = nullEvent;
return 0;
}
/* if we got this far, then we have a reasonably valid keystroke */
mustRemember = true;
return 0;
}
/**********************************************************
* SDcdev::ItemHit
*
* This handles dialog item hits.
**********************************************************/
long SDcdev::ItemHit(short theItem)
{
ControlHandle theControl;
short controlValue, initialRect;
Rect box, save;
long theLong, theTime = 0;
Str255 theString;
Boolean drawOtherAsWell = false;
GetDItem(fDialog, fLastItem - initDev + theItem, &controlValue, (Handle *) &theControl, &box);
switch (theItem) {
case sleepNow:
initialRect = sleepNowRect;
GlobalToLocal(&fEvent->where);
if (PtInRect(fEvent->where, &smallSNRect[topLeftRect])) {
if (neverSleepRect == topLeftRect) {
neverSleepRect = sleepNowRect;
drawOtherAsWell = true;
}
sleepNowRect = topLeftRect;
} else if (PtInRect(fEvent->where, &smallSNRect[topRightRect])) {
if (neverSleepRect == topRightRect) {
neverSleepRect = sleepNowRect;
drawOtherAsWell = true;
}
sleepNowRect = topRightRect;
} else if (PtInRect(fEvent->where, &smallSNRect[botRightRect])) {
if (neverSleepRect == botRightRect) {
neverSleepRect = sleepNowRect;
drawOtherAsWell = true;
}
sleepNowRect = botRightRect;
} else if (PtInRect(fEvent->where, &smallSNRect[botLeftRect])) {
if (neverSleepRect == botLeftRect) {
neverSleepRect = sleepNowRect;
drawOtherAsWell = true;
}
sleepNowRect = botLeftRect;
} else { /* This can't happen. */
SysBeep(5);
break;
}
if (initialRect == sleepNowRect) /* Nothing changed */
break;
/* Draw the new picture */
SetDItem(fDialog, fLastItem - initDev + sleepNow,
controlValue, pictHandles[sleepNowRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
if (drawOtherAsWell) {
GetDItem(fDialog, fLastItem - initDev + neverSleep, &controlValue, (Handle *) &theControl, &box);
SetDItem(fDialog, fLastItem - initDev + neverSleep, controlValue,
pictHandles[neverSleepRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
}
RememberValues();
break;
case neverSleep:
initialRect = neverSleepRect;
GlobalToLocal(&fEvent->where);
if (PtInRect(fEvent->where, &smallNSRect[topLeftRect])) {
if (sleepNowRect == topLeftRect) {
sleepNowRect = neverSleepRect;
drawOtherAsWell = true;
}
neverSleepRect = topLeftRect;
} else if (PtInRect(fEvent->where, &smallNSRect[topRightRect])) {
if (sleepNowRect == topRightRect) {
sleepNowRect = neverSleepRect;
drawOtherAsWell = true;
}
neverSleepRect = topRightRect;
} else if (PtInRect(fEvent->where, &smallNSRect[botRightRect])) {
if (sleepNowRect == botRightRect) {
sleepNowRect = neverSleepRect;
drawOtherAsWell = true;
}
neverSleepRect = botRightRect;
} else if (PtInRect(fEvent->where, &smallNSRect[botLeftRect])) {
if (sleepNowRect == botLeftRect) {
sleepNowRect = neverSleepRect;
drawOtherAsWell = true;
}
neverSleepRect = botLeftRect;
} else { /* This can't happen. */
SysBeep(5);
break;
}
if (initialRect == neverSleepRect) /* Nothing changed */
break;
/* Draw the new picture */
SetDItem(fDialog, fLastItem - initDev + neverSleep,
controlValue, pictHandles[neverSleepRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[neverSleepRect], &box);
if (drawOtherAsWell) {
GetDItem(fDialog, fLastItem - initDev + sleepNow, &controlValue, (Handle *) &theControl, &box);
SetDItem(fDialog, fLastItem - initDev + sleepNow, controlValue,
pictHandles[sleepNowRect], &box);
InsetRect(&box, 3, 3);
EraseRect(&box);
InsetRect(&box, -3, -3);
DrawPicture((PicHandle) pictHandles[sleepNowRect], &box);
}
RememberValues();
break;
case idleRect:
if (Gestalt('sysv', &theLong) != noErr && theLong < 0x700)
break;
save = box;
GlobalToLocal(&fEvent->where);
GetDItem(fDialog, fLastItem - initDev + idleMins,
&controlValue, (Handle *) &theControl, &box);
GetIText((Handle) theControl, theString);
StringToNum(theString, &theLong);
theTime = LMGetTicks() - 16;
if (PtInRect(fEvent->where, &smIdleRect)) { // higher!!!
DrawPicture((PicHandle) pictHandles[upArrow], &save);
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
do {
SystemTask();
if (LMGetTicks() - theTime > 15) {
theTime = LMGetTicks();
if (theLong < 10) {
NumToString(theLong += 1, theString);
SetIText((Handle) theControl, theString);
if (theLong == 10)
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
} else if (theLong < 60) {
NumToString(theLong = (theLong > 55 ? 60 : theLong + 5), theString);
SetIText((Handle) theControl, theString);
}
}
} while (Button());
} else { // lower.
DrawPicture((PicHandle) pictHandles[dnArrow], &save);
SelIText(fDialog, fLastItem - initDev + idleMins, 0, 32767);
do {
SystemTask();
if (LMGetTicks() - theTime > 15) {
theTime = LMGetTicks();
if (theLong > 10) {
NumToString(theLong = (theLong < 15 ? 10 : theLong - 5), theString);
SetIText((Handle) theControl, theString);
} else if (theLong > 1) {
NumToString(theLong -= 1, theString);
SetIText((Handle) theControl, theString);
}
}
} while (Button());
}
box = save;
box.left += 3;
box.right -= 3;
box.top += 3;
box.bottom -= 3;
EraseRect(&box);
DrawPicture((PicHandle) pictHandles[noArrow], &save);
RememberValues();
break;
case screenOn:
case startupIcon:
controlValue = GetCtlValue(theControl);
controlValue = !controlValue;
SetCtlValue(theControl, controlValue);
RememberValues();
break;
default:
break;
}
return 0;
}
/**********************************************************
* SDcdev::RememberValues
*
* This saves the information in the CNFG resource, and,
* if the Basic Black INIT is loaded, sets the values of
* its patch globals in memory.
**********************************************************/
void SDcdev::RememberValues(void)
{
PrefStructureHandle prefHandle;
ControlHandle theControl;
short controlValue, VRefNum, theResFile, thePrefsFile;
long theLong, result, directoryID;
Rect box;
Str255 theString;
PatchGlobalsPtr pgPtr;
/* Load the preferences */
theResFile = CurResFile();
if (Gestalt(gestaltFindFolderAttr, &result) == noErr)
if (gestaltFindFolderPresent == 0)
if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder,
&VRefNum, &directoryID) == noErr)
if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
"\pScreenDaemon Prefs", fsCurPerm)) != -1) {
prefHandle = (PrefStructureHandle) GetIndResource('PREF', 1);
if (ResError()) { /* fixme */
/*Error(cdevResErr);*/
return;
}
/* save the idle time information */
GetDItem(fDialog, fLastItem - initDev + idleMins, &controlValue, (Handle *) &theControl, &box);
GetIText((Handle) theControl, theString);
StringToNum(theString, &theLong);
theLong = (theLong < 1 ? 1 : theLong);
theLong = (theLong > 60 ? 60 : theLong);
NumToString(theLong, theString);
(**prefHandle).idleTime = (short) theLong;
/* save the sleep corner information */
(**prefHandle).sleepNowCorner = sleepNowRect;
/* save the wake corner information */
(**prefHandle).sleepNeverCorner = neverSleepRect;
/* save the "screen saver on" information */
GetDItem(fDialog, fLastItem - initDev + screenOn, &controlValue, (Handle *) &theControl, &box);
(**prefHandle).saverOn = GetCtlValue(theControl);
/* save the "Startup Icon" information */
GetDItem(fDialog, fLastItem - initDev + startupIcon, &controlValue, (Handle *) &theControl, &box);
(**prefHandle).showIcon = GetCtlValue(theControl);
/* If the extension's installed, then update it's patch globals */
if (Gestalt('sDmn', (long *) &pgPtr) == noErr) {
if (pgPtr->pgVersion == (**prefHandle).version) {
pgPtr->pgMustSleep = false;
pgPtr->pgSleepRect = (**prefHandle).sleepNowCorner;
pgPtr->pgWakeRect = (**prefHandle).sleepNeverCorner;
pgPtr->pgIdleTicks = (long) (**prefHandle).idleTime * 3600;
pgPtr->pgMustSave = (**prefHandle).saverOn;
}
}
}
/* Clean up */
ChangedResource((Handle) prefHandle);
WriteResource((Handle) prefHandle);
ReleaseResource((Handle) prefHandle);
CloseResFile(thePrefsFile);
UseResFile(theResFile);
}
/*********************************************************************
* OpenPrefsFile:
*
* Open up our prefs file. If there are problems, then create a new
* prefs file.
*
*********************************************************************/
Boolean OpenPrefsFile()
{
long result, directoryID;
FInfo fileInfo;
short VRefNum, theResFile, thePrefsFile, resAttrs;
Boolean prefsFileOpen = false;
Handle prefHandle, tmplHandle;
theResFile = CurResFile();
/* Read in our prefs, and do a sick amount of error checking (bleah!) */
if (Gestalt(gestaltFindFolderAttr, &result) == noErr) {
if (result & Bit(gestaltFindFolderPresent)) {
if (FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
&VRefNum, &directoryID) == noErr) {
if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
"\pScreenDaemon Prefs", fsCurPerm)) != -1) {
/* Cool. We opened the file! Twiddle our watch variable... */
prefsFileOpen = true;
} else { /* No go. Try to create a new prefs file. */
HCreateResFile(VRefNum, directoryID, "\pScreenDaemon Prefs");
HGetFInfo(VRefNum, directoryID, "\pScreenDaemon Prefs", &fileInfo);
fileInfo.fdType = 'pref';
fileInfo.fdCreator = 'sDmn';
HSetFInfo(VRefNum, directoryID, "\pScreenDaemon Prefs", &fileInfo);
if ((thePrefsFile = HOpenResFile(VRefNum, directoryID,
"\pScreenDaemon Prefs", fsCurPerm)) != -1) {
/* We opened up the new file okay. */
prefsFileOpen = true;
/* Get preferences resource */
UseResFile(theResFile);
prefHandle = GetIndResource('PREF', 1);
resAttrs = GetResAttrs(prefHandle);
DetachResource(prefHandle);
/* Write preferences resource */
UseResFile(thePrefsFile);
AddResource(prefHandle, 'PREF', 128, "\pPrefs");
SetResAttrs(prefHandle, resAttrs);
ChangedResource(prefHandle);
WriteResource(prefHandle);
ReleaseResource(prefHandle);
/* Get template resource */
UseResFile(theResFile);
tmplHandle = GetIndResource('TMPL', 1);
resAttrs = GetResAttrs(tmplHandle);
DetachResource(tmplHandle);
/* Write template resource */
UseResFile(thePrefsFile);
AddResource(tmplHandle, 'TMPL', 128, "\pPREF");
SetResAttrs(tmplHandle, resAttrs);
ChangedResource(tmplHandle);
WriteResource(tmplHandle);
ReleaseResource(tmplHandle);
}
}
}
}
}
return prefsFileOpen;
}